package com.chenshi.rabbitmq.listener;


import com.chenshi.rabbitmq.config.RabbitmqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 *
 * @author chenyq
 * @date 2023/12/12
 */
@Component
public class MyRabbitListener {

    public static MessageProperties messageProperties;

    /*
    * 简单模式的消息接受
    * */
    @RabbitListener(queues = RabbitmqConfig.SIMPLE_QUEUE)
    public void receiveSimpleQueue(Message message){
        //业务逻辑
        getMsgBody(message);
        //消息参数
        getMsgProperties(messageProperties);
    }


    /*
    * 发布/订阅模式的消息接收=======================================================================================
    * */
    @RabbitListener(queues = RabbitmqConfig.FANOUT_QUEUE_1)
    public void receiveFanoutQueue1(Message message){
        //业务逻辑
        getMsgBody(message);
        //消息参数
        getMsgProperties(messageProperties);
    }

    @RabbitListener(queues = RabbitmqConfig.FANOUT_QUEUE_2)
    public void receiveFanoutQueue2(Message message){
        //业务逻辑
        getMsgBody(message);
        //消息参数
        getMsgProperties(messageProperties);
    }

    /*
    * 通配符模式的消息接受=========================================================================================
    * queues: 使用{}包裹起来用，隔开能声明并监听多个队列
    * */
    @RabbitListener(queues = {RabbitmqConfig.TOPIC_QUEUE_1,RabbitmqConfig.TOPIC_QUEUE_2,RabbitmqConfig.TOPIC_QUEUE_3})
    public void receiveTopicQueue(Message message){
        //业务逻辑
        getMsgBody(message);
        //消息参数
        getMsgProperties(messageProperties);
    }



    /*
    * 提取方法====================================================================================================
    * */
    public static void getMsgBody(Message message){
        System.out.println((new String(message.getBody())));
    }

    public static void getMsgProperties(MessageProperties messageProperties){
        System.out.println(messageProperties.getMessageId());
        System.out.println(messageProperties.getDeliveryTag());
        System.out.println(messageProperties.getReceivedRoutingKey());
        System.out.println(messageProperties.getConsumerTag());
    }

}
